package com.example.travelgd.controller;

import com.example.travelgd.entity.Carousel;
import com.example.travelgd.service.CarouselService;
import com.example.travelgd.util.FwResult;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/carousel")
@CrossOrigin(origins = "*", allowedHeaders = "*", maxAge = 3600)
@Tag(name = "CarouselController", description = "轮播图管理")
public class CarouselController {

    @Autowired
    private CarouselService carouselService;

    @GetMapping("/list")
    @Operation(summary = "获取轮播图列表")
    public FwResult<List<Carousel>> getCarouselList() {
        log.info("开始获取轮播图列表");
        long startTime = System.currentTimeMillis();
        try {
            List<Carousel> carouselList = carouselService.getCarouselList();
            long endTime = System.currentTimeMillis();
            log.info("获取轮播图列表耗时: {}ms, 数量: {}, 是否从缓存获取: {}",
                    (endTime - startTime),
                    carouselList.size(),
                    carouselService.isCacheValid());
            return FwResult.ok(carouselList);
        } catch (Exception e) {
            log.error("获取轮播图列表失败", e);
            return FwResult.failedMsg("获取轮播图列表失败: " + e.getMessage());
        }
    }

    @PostMapping
    @Operation(summary = "添加轮播图")
    public FwResult<Void> addCarousel(@RequestBody Carousel carousel) {
        log.info("开始添加轮播图: {}", carousel);
        try {
            // 参数校验
            if (carousel == null || carousel.getImageUrl() == null || carousel.getImageUrl().trim().isEmpty()) {
                return FwResult.failedMsg("轮播图信息不完整");
            }
            return carouselService.addCarousel(carousel);
        } catch (Exception e) {
            log.error("添加轮播图失败", e);
            return FwResult.failedMsg("添加轮播图失败: " + e.getMessage());
        }
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新轮播图")
    public FwResult<Void> updateCarousel(@PathVariable Long id, @RequestBody Carousel carousel) {
        log.info("开始更新轮播图, id: {}, carousel: {}", id, carousel);
        try {
            // 参数校验
            if (id == null || carousel == null) {
                return FwResult.failedMsg("参数不完整");
            }
            carousel.setId(id);
            return carouselService.updateCarousel(carousel);
        } catch (Exception e) {
            log.error("更新轮播图失败", e);
            return FwResult.failedMsg("更新轮播图失败: " + e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除轮播图")
    public FwResult<Void> deleteCarousel(@PathVariable Long id) {
        log.info("开始删除轮播图, id: {}", id);
        try {
            if (id == null) {
                return FwResult.failedMsg("轮播图ID不能为空");
            }
            return carouselService.deleteCarousel(id);
        } catch (Exception e) {
            log.error("删除轮播图失败", e);
            return FwResult.failedMsg("删除轮播图失败: " + e.getMessage());
        }
    }

    @DeleteMapping("/cache")
    @Operation(summary = "清除轮播图缓存")
    public FwResult<Void> clearCache() {
        log.info("开始清除轮播图缓存");
        try {
            carouselService.clearCache();
            return FwResult.ok();
        } catch (Exception e) {
            log.error("清除轮播图缓存失败", e);
            return FwResult.failedMsg("清除轮播图缓存失败: " + e.getMessage());
        }
    }

    @PostMapping("/cache/refresh")
    @Operation(summary = "刷新轮播图缓存")
    public FwResult<Void> refreshCache() {
        log.info("开始刷新轮播图缓存");
        try {
            carouselService.refreshCache();
            return FwResult.ok();
        } catch (Exception e) {
            log.error("刷新轮播图缓存失败", e);
            return FwResult.failedMsg("刷新轮播图缓存失败: " + e.getMessage());
        }
    }

    @GetMapping("/cache/status")
    @Operation(summary = "获取缓存状态")
    public FwResult<Boolean> getCacheStatus() {
        log.info("开始获取轮播图缓存状态");
        try {
            boolean isValid = carouselService.isCacheValid();
            log.info("轮播图缓存状态: {}", isValid ? "有效" : "无效");
            return FwResult.ok(isValid);
        } catch (Exception e) {
            log.error("获取缓存状态失败", e);
            return FwResult.failedMsg("获取缓存状态失败: " + e.getMessage());
        }
    }
}